<?php

namespace App\HttpController\Open;


use App\HttpController\Base;
use App\Model\Service\LoginService;

class Login extends Base
{

    protected bool $needToken = false;

    protected function initService(array $postParams, array $loginUser): void
    {
        $this->service = new LoginService($postParams, $loginUser);
    }

    public function selectBiz(): bool
    {

        $is = $this->service->selectBiz();

        if (!$is) {
            return $this->error($this->service->getError());
        }

        return $this->success($this->service->getSuccess());
    }

    public function handle(): bool
    {

        $is = $this->service->login();

        if (!$is) {
            return $this->error($this->service->getError());
        }

        return $this->success($this->service->getSuccess());
    }

    public function exit(): bool
    {

        $token = $this->request()->getHeader('token')[0] ?? '';

        $token && $this->service->exit($token);

        return $this->success();
    }

    protected function getValidate(string $actionName): array
    {
        switch ($actionName) {
            case 'exit':
                return [
                    'api_come' => 'required,notEmpty'
                ];
            case 'selectBiz':
                return [
                    'account' => 'required,notEmpty',
                    'api_come' => 'required,notEmpty',
                ];
            case 'handle':
            default:
                return [
                    'base_id' => 'required,notEmpty',
                    'user_id' => 'required,notEmpty',
                    'password' => 'required,notEmpty',
                    'api_come' => 'required,notEmpty',
                ];
        }
    }


}